package indi.lddsdu.util;

import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.concurrent.ExecutorService;


/**
 * Created by jack on 18/6/9.
 */
public class SearchUtil {

    /**
     * 获取IndexSearcher对象(适合单索引目录查询)
     * @param indexpath     索引目录
     * @param service       ????
     * @param realTime      boolean
     * @return
     * @throws IOException
     */
    public static IndexSearcher getIndexSearcher(String indexpath, ExecutorService service, boolean realTime) throws IOException {
        DirectoryReader directoryReader = DirectoryReader.open(
                IndexUtil.getIndexWriter(indexpath, true),
                realTime);
        IndexSearcher searcher = new IndexSearcher(directoryReader);
        if(service != null){
            service.shutdown();
        }
        return searcher;
    }



    public static IndexSearcher getIndexSearcher(String dirPath) throws IOException {
        Directory directory = FSDirectory.open(Paths.get(dirPath));
        return getIndexSearcher(directory);
    }


    public static IndexSearcher getIndexSearcher(Directory directory) throws IOException {
        DirectoryReader directoryReader = DirectoryReader.open(directory);
        IndexSearcher indexSearcher = new IndexSearcher(directoryReader);
        return indexSearcher;
    }
}
